/*
 * 文件名：ValiUtil.java
 * 版权：CopyRight 2000-2010 Huawei Technologies Co., Ltd.All Righs Reserved.
 * 描述：
 * 修改人：g69866
 * 修改时间：2011-10-7
 * 修改内容：新增
 */
package com.hxt.framework.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.hxt.util.YmlUtil;

public class DbUtil {

	// 获取数据库连接
	private  static Connection getConnection() {

		final String DBDRIVER = "com.mysql.jdbc.Driver";// 驱动类类名

		final String DBNAME = "zbak";// 数据库名

//		final String DBURL = "jdbc:mysql://127.0.0.1:3306/" + DBNAME;// 连接URL
//
//		final String DBUSER = "root";// 数据库用户名
//
//		final String DBPASSWORD = "root";// 数据库密码
		
		final String DBURL = (String) YmlUtil.getValue("application.yml", "jiaparts.db.xt.xa-properties.url");// 连接URL
		final String DBUSER = (String) YmlUtil.getValue("application.yml", "jiaparts.db.xt.xa-properties.user");// 数据库用户名
		final String DBPASSWORD = (String) YmlUtil.getValue("application.yml", "jiaparts.db.xt.xa-properties.password");// 数据库密码

		Connection conn = null;

		try {

			Class.forName(DBDRIVER);// 注册驱动

			conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象

		} catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常

			e.printStackTrace();

		} catch (SQLException e) {// 捕获SQL异常

			e.printStackTrace();

		}

		return conn;
	}

	public static void insert(String sql) {
		PreparedStatement ps = null;
		Connection conn = null;

		try {

			conn = getConnection();
			
			ps = conn.prepareStatement(sql);

			ps.executeUpdate(sql);

		} catch (SQLException sqle) {

			sqle.printStackTrace();

		} finally {
			try {
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
